//封装千分位分隔函数
function formatNumberToThousands(num: number): string {
  if (isNaN(num) || typeof num != "number") {
    throw new Error("必须是一个有效数字");
  }

  const [interger, decimal] = num.toString().split(".");

  let result = "";
  for (let i = interger.length - 1; i >= 0; i--) {
    result = interger[i] + result;

    if ((interger.length - i) % 3 === 0 && i !== 0) {
      result = "," + result;
    }
  }
  return decimal ? `${result}.${decimal}` : result;
}
export default formatNumberToThousands;